/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function TransportTariff() {
    this.javaClass = "limousine.vo.TransportTariff";
    this.tableId = 0;
    this.transportTariffId = "";
    this.tariffOrder = 0;
    this.distination = "";
    this.areaType = 0;
    this.areaTypeName = "";
    this.areaCover = "";
    this.timeUsage = "";
    this.postalCodeCover = "";
    this.rateSedan = 0;
    this.rateVan = 0;
}
function edit(id) {
    try {
        addProcessing('Please wait...');
        formEntry();
        var trans = service.findById(id);
        $d("tableId").value = trans.tableId;
        $d("transportId").value = trans.transportTariffId;
        $d("distination").value = trans.distination;
        $d("areaType").value = trans.areaType;
        $d("areaCover").value = trans.areaCover;
        $d("timeUsage").value = trans.timeUsage;
        $d("postalCodeCover").value = trans.postalCodeCover;
        $d("rateOfSedan").value = trans.rateSedan;
        $d("rateOfVan").value = trans.rateVan;
        $d("orderId").value = trans.tariffOrder;
        $d("bSave").setAttribute("onclick","save()");
        getLookupList("AREA_TYPE","areaType",trans.areaType);
    } catch(err) {
        alert(err);
    }
    clearAllMsg();
}
function save() {
    try {
        addProcessing('Please wait...');
        var trans = new TransportTariff();
        trans.tableId = $d("tableId").value;
        trans.transportTariffId = $d("transportId").value;
        trans.distination = $d("distination").value;
        trans.areaType = parseInt($d("areaType").value);
        trans.areaCover = $d("areaCover").value;
        trans.timeUsage = $d("timeUsage").value;
        trans.postalCodeCover = $d("postalCodeCover").value;
        trans.rateSedan = parseInt($d("rateOfSedan").value);
        trans.rateVan = parseInt($d("rateOfVan").value);
        trans.tariffOrder = parseInt($d("orderId").value);
        trans.createBy = $d("createBy").value;
        trans.createDate = "0000-00-00 00:00:00";
        trans.updateBy = $d("updateBy").value;
        trans.updateDate = "0000-00-00 00:00:00";
        if( verifyForm() == true ) {
        addProcessing('Please wait...');
        var ret = service.update(trans);
        if( ret == "true" ) {
            switchProcess(1,"Save Transport Tariff Success.","formSearch()");
        } else {
            switchProcess(0,"Save Transport Tariff fail.","");
        }
        }
    } catch(err) {
        alert(err);
    }
    //clearAllMsg();
}
function remove(transId) {
    try {
        addProcessing('Please wait...');
        service.remove(transId);
        search(0);
    } catch(err) {
        alert(err);
    }
    clearAllMsg();
}
function search(offset) {
    try {
        addProcessing('Please wait...');
        var ls = service.search(offset, $d("tDistination").value);
        var jTable = new JTable();
        jTable.setTableId("tableData");
        jTable.setColumns(["Destination", "Area Name", "Time Usage", "Rate Sedan", "Rate Van"]);
        jTable.setFields(["distination","areaTypeName","timeUsage", "rateSedan", "rateVan"]);
        jTable.setEditFunction("edit");
        jTable.setDeleteFunction("remove");
        jTable.setDataId("tableId");
        jTable.setCount(ls.size());
        jTable.setOffset(offset);
        jTable.setDataPageSize(20);
        jTable.setPageLink("search");
        jTable.setTableHeader();
        jTable.loadData(ls);
    } catch(err) {
        alert(err);
    }
    clearAllMsg();
}
function insert() {
    try {
        addProcessing('Please wait...');
        //alert('method insert transport Tariff');
        var trans = new TransportTariff();
        trans.tableId = $d("tableId").value;
        trans.transportTariffId = $d("transportId").value;
        trans.distination = $d("distination").value;
        trans.areaType = parseInt($d("areaType").value);
        trans.areaCover = $d("areaCover").value;
        trans.timeUsage = $d("timeUsage").value;
        trans.postalCodeCover = $d("postalCodeCover").value;
        trans.rateSedan = parseInt($d("rateOfSedan").value);
        trans.rateVan = parseInt($d("rateOfVan").value);
        trans.tariffOrder = parseInt($d("orderId").value);
        trans.createBy = $d("createBy").value;
        trans.createDate = "0000-00-00 00:00:00";
        trans.updateBy = $d("updateBy").value;
        trans.updateDate = "0000-00-00 00:00:00";
        if( verifyForm() == true ) {
        addProcessing('Please wait...');
        var ret = service.save(trans);
        if( ret == "true" ) {
            switchProcess(1,"Add Transport Tariff completed.","formSearch()");
        } else {
            switchProcess(0,"Add Transport Tariff fail.","");
        }
    }
    } catch(err) {
        alert(err);
    }
    //clearAllMsg();
}
        
function setInsert() {
    try {
        formEntry();
        var searchd = $d("tableId").value;
        $d("actionForm").reset();
        //$d("customerId").value = searchd;
        $d("tableId").value = service.requestId(); 
        $d("bSave").setAttribute("onclick","insert()");        
        getLookupList("AREA_TYPE","areaType","");
    } catch(err) {
        alert("setInsert err:"+err);
    }
    clearAllMsg();
}
         
function formEntry() {
    try {
        addProcessing('Please wait...');
        $d("formSearch").style.visibility = "hidden";
        var formEntry = $d("formEntry");
        formEntry.style.visibility = "visible";
        var panel = $d("panel");
        formEntry.style.top = panel.style.top;
        formEntry.style.left = panel.style.left;
    } catch(err) {
        alert(err);
    }
}
function changeSearch() {
    try {
        addProcessing('Please wait...');
        $d("formEntry").style.visibility = "hidden";
        var search = $d("formSearch");
        search.style.visibility = "visible";
        var panel = $d("panel");
        search.style.top = panel.style.top;
        search.style.left = panel.style.left;
    } catch(err) {
        alert(err);
    }
}
function verifyForm() {
    try {
        addProcessing('Please wait...');
        var v = new Validation();
        v.addField("name='transportId',label='Transport Id',min=4,max=25,type='string',require=true");
        v.addField("name='distination',label='Destination',min=1,max=255,type='string',require=true");
        v.addField("name='areaType',label='Area Type',min=0,max=255,type='int',require=true");
        v.addField("name='areaCover',label='Area Cover',min=1,max=50,type='string',require=true");
        v.addField("name='timeUsage',label='Time',min=1,max=5,type='string',require=true");
         
        
        v.addField("name='postalCodeCover',label='Postal Code Cover',min=1,max=50,type='string',require=true");
        v.addField("name='rateOfSedan',label='Rate Of Sedan',type='int',min=0,max=30000,require=true");
        v.addField("name='rateOfVan',label='Rate Of Van',type='int',min=5,max=3000,require=true");
        v.addField("name='orderId',label='Order Id',type='int',min=0,max=3000,require=true");
        var str = v.execute();
        if ( str == "" ) {
            return true;
        } else {
            addSysMsg(0,str);
            return false;
        }
    } catch(err) {
        alert(err);
    }
}
function formSearch() {
    clearAllMsg();
    document.getElementById("fieldSetLabel").textContent="Transport Tariff Search";    
    try {
        $d("formEntry").style.visibility = "hidden";
        var search = $d("formSearch");
        search.style.visibility = "visible";
        var panel = $d("panel");
        search.style.top = panel.style.top;
        search.style.left = panel.style.left;
    } catch(err) {
        alert("formSearch err:"+err);
    }
}
function getLookupList(lookupGroupId,propertieId,selectedValue) {    
    var list = serviceLookup.getLookupType(lookupGroupId);
    var i=0;
    for( i =0; i < list.size(); i++ ) {
        var txt = list.get(i).lookupName;
        var val = list.get(i).lookupValue;
        
        if(val==selectedValue){
            $d(propertieId).options[i] = new Option(txt, val, false, true);
        }else{
            $d(propertieId).options[i] = new Option(txt, val, false, false);
        }
    }   
}
function switchProcess(type,msg,method){
    if(type == 0){//fail
        addSysMsg(0,msg);
    }else{
        addProcessing(msg);
        setTimeout(method,2500);        
    }    
}